<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图片处理工具 - Apollo管理系统</title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Inter', sans-serif;
            background: #f8fafc;
            color: #333333;
            line-height: 1.6;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }
        
        .header {
            background: #ffffff;
            padding: 30px;
            border-radius: 12px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            margin-bottom: 30px;
            text-align: center;
        }
        
        .header h1 {
            color: #1e3a8a;
            font-size: 2.5rem;
            margin-bottom: 10px;
        }
        
        .header p {
            color: #64748b;
            font-size: 1.1rem;
        }
        
        .tool-section {
            background: #ffffff;
            padding: 30px;
            border-radius: 12px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            margin-bottom: 30px;
        }
        
        .section-title {
            color: #1e3a8a;
            font-size: 1.5rem;
            margin-bottom: 20px;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        .form-label {
            display: block;
            margin-bottom: 8px;
            color: #333333;
            font-weight: 600;
            font-size: 0.9rem;
        }
        
        .form-control {
            width: 100%;
            padding: 12px 16px;
            background: #ffffff;
            border: 1px solid rgba(0, 0, 0, 0.2);
            border-radius: 6px;
            color: #333333;
            font-size: 1rem;
            transition: all 0.3s ease;
        }
        
        .form-control:focus {
            outline: none;
            border-color: #1e3a8a;
            box-shadow: 0 0 0 3px rgba(30, 58, 138, 0.1);
        }
        
        .btn {
            background: linear-gradient(135deg, #1e3a8a, #3b82f6);
            color: #fff;
            border: none;
            padding: 12px 24px;
            border-radius: 6px;
            cursor: pointer;
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: 1px;
            transition: all 0.3s ease;
            display: inline-flex;
            align-items: center;
            gap: 8px;
        }
        
        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(30, 58, 138, 0.4);
        }
        
        .btn:disabled {
            opacity: 0.6;
            cursor: not-allowed;
            transform: none;
        }
        
        .btn-secondary {
            background: transparent;
            color: #1e3a8a;
            border: 2px solid #1e3a8a;
        }
        
        .btn-secondary:hover {
            background: #1e3a8a;
            color: #fff;
        }
        
        .grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 20px;
        }
        
        .image-preview {
            border: 2px dashed #cbd5e1;
            border-radius: 8px;
            padding: 20px;
            text-align: center;
            background: #f8fafc;
        }
        
        .image-preview img {
            max-width: 100%;
            max-height: 300px;
            border-radius: 8px;
            margin-bottom: 10px;
        }
        
        .image-info {
            background: #f1f5f9;
            padding: 15px;
            border-radius: 8px;
            margin-top: 15px;
        }
        
        .info-item {
            display: flex;
            justify-content: space-between;
            margin-bottom: 8px;
        }
        
        .info-label {
            font-weight: 600;
            color: #1e3a8a;
        }
        
        .result-section {
            background: #f0f9ff;
            border: 1px solid #0ea5e9;
            border-radius: 8px;
            padding: 20px;
            margin-top: 20px;
        }
        
        .success {
            color: #059669;
            background: #ecfdf5;
            border-color: #10b981;
        }
        
        .error {
            color: #dc2626;
            background: #fef2f2;
            border-color: #ef4444;
        }
        
        .url-display {
            background: #ffffff;
            border: 1px solid #d1d5db;
            border-radius: 6px;
            padding: 10px;
            margin-top: 10px;
            font-family: monospace;
            word-break: break-all;
        }
        
        .copy-btn {
            margin-left: 10px;
            padding: 5px 10px;
            font-size: 0.8rem;
        }
        
        .loading {
            display: none;
            text-align: center;
            padding: 20px;
        }
        
        .loading i {
            font-size: 2rem;
            color: #1e3a8a;
            animation: spin 1s linear infinite;
        }
        
        @keyframes spin {
            from { transform: rotate(0deg); }
            to { transform: rotate(360deg); }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1><i class="fas fa-image"></i> 图片处理工具</h1>
            <p>上传图片并调整分辨率，提升图片质量</p>
        </div>
        
        <!-- 文件上传区域 -->
        <div class="tool-section">
            <h2 class="section-title">
                <i class="fas fa-upload"></i>
                上传图片
            </h2>
            <div class="form-group">
                <label class="form-label">选择图片文件</label>
                <input type="file" id="fileInput" class="form-control" accept="image/*">
            </div>
            <button type="button" class="btn" onclick="uploadFile()">
                <i class="fas fa-cloud-upload-alt"></i>
                上传图片
            </button>
        </div>
        
        <!-- 已有文件处理区域 -->
        <div class="tool-section">
            <h2 class="section-title">
                <i class="fas fa-cog"></i>
                处理已有图片
            </h2>
            <div class="form-group">
                <label class="form-label">文件名</label>
                <input type="text" id="existingFilename" class="form-control" 
                       placeholder="输入文件名，例如：0430adfae6ea4890ad41d6dfcb7d22c3"
                       value="0430adfae6ea4890ad41d6dfcb7d22c3">
            </div>
            <div class="grid">
                <div class="form-group">
                    <label class="form-label">目标宽度 (像素)</label>
                    <input type="number" id="targetWidth" class="form-control" value="1920" min="100" max="4000">
                </div>
                <div class="form-group">
                    <label class="form-label">目标高度 (像素)</label>
                    <input type="number" id="targetHeight" class="form-control" value="1080" min="100" max="4000">
                </div>
                <div class="form-group">
                    <label class="form-label">图片质量 (0.1-1.0)</label>
                    <input type="number" id="quality" class="form-control" value="0.9" min="0.1" max="1.0" step="0.1">
                </div>
            </div>
            <div style="margin-top: 20px;">
                <button type="button" class="btn" onclick="getImageInfo()">
                    <i class="fas fa-info-circle"></i>
                    获取图片信息
                </button>
                <button type="button" class="btn" onclick="resizeImage()">
                    <i class="fas fa-expand-arrows-alt"></i>
                    调整分辨率
                </button>
                <button type="button" class="btn btn-secondary" onclick="enhanceImage()">
                    <i class="fas fa-magic"></i>
                    一键增强
                </button>
            </div>
        </div>
        
        <!-- 加载状态 -->
        <div class="loading" id="loading">
            <i class="fas fa-spinner"></i>
            <p>处理中，请稍候...</p>
        </div>
        
        <!-- 结果显示区域 -->
        <div id="resultSection" style="display: none;">
            <div class="tool-section">
                <h2 class="section-title">
                    <i class="fas fa-check-circle"></i>
                    处理结果
                </h2>
                <div id="resultContent"></div>
            </div>
        </div>
        
        <!-- 图片预览区域 -->
        <div id="previewSection" style="display: none;">
            <div class="tool-section">
                <h2 class="section-title">
                    <i class="fas fa-eye"></i>
                    图片预览
                </h2>
                <div class="grid">
                    <div>
                        <h3>原图</h3>
                        <div class="image-preview" id="originalPreview"></div>
                    </div>
                    <div>
                        <h3>处理后</h3>
                        <div class="image-preview" id="processedPreview"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <script>
        const API_BASE = '/api/file';
        
        function showLoading() {
            document.getElementById('loading').style.display = 'block';
        }
        
        function hideLoading() {
            document.getElementById('loading').style.display = 'none';
        }
        
        function showResult(content, type = 'success') {
            const resultSection = document.getElementById('resultSection');
            const resultContent = document.getElementById('resultContent');
            
            resultContent.innerHTML = `<div class="result-section ${type}">${content}</div>`;
            resultSection.style.display = 'block';
            
            // 滚动到结果区域
            resultSection.scrollIntoView({ behavior: 'smooth' });
        }
        
        async function uploadFile() {
            const fileInput = document.getElementById('fileInput');
            const file = fileInput.files[0];
            
            if (!file) {
                alert('请选择文件');
                return;
            }
            
            showLoading();
            
            try {
                const formData = new FormData();
                formData.append('file', file);
                
                const response = await fetch(`${API_BASE}/upload`, {
                    method: 'POST',
                    body: formData
                });
                
                const result = await response.json();
                
                if (result.code === 200) {
                    document.getElementById('existingFilename').value = result.data;
                    showResult(`
                        <h3><i class="fas fa-check-circle"></i> 文件上传成功</h3>
                        <p>文件名: <strong>${result.data}</strong></p>
                        <div class="url-display">
                            ${window.location.origin}${API_BASE}/get?filename=${result.data}
                            <button class="btn copy-btn" onclick="copyToClipboard('${window.location.origin}${API_BASE}/get?filename=${result.data}')">
                                <i class="fas fa-copy"></i> 复制
                            </button>
                        </div>
                    `);
                    
                    // 自动获取图片信息
                    setTimeout(() => getImageInfo(), 1000);
                } else {
                    showResult(`<h3><i class="fas fa-exclamation-triangle"></i> 上传失败</h3><p>${result.message}</p>`, 'error');
                }
            } catch (error) {
                showResult(`<h3><i class="fas fa-exclamation-triangle"></i> 上传失败</h3><p>${error.message}</p>`, 'error');
            } finally {
                hideLoading();
            }
        }
        
        async function getImageInfo() {
            const filename = document.getElementById('existingFilename').value.trim();
            
            if (!filename) {
                alert('请输入文件名');
                return;
            }
            
            showLoading();
            
            try {
                const response = await fetch(`${API_BASE}/info?filename=${filename}`);
                const result = await response.json();
                
                if (result.code === 200) {
                    const info = result.data;
                    showResult(`
                        <h3><i class="fas fa-info-circle"></i> 图片信息</h3>
                        <div class="image-info">
                            <div class="info-item">
                                <span class="info-label">宽度:</span>
                                <span>${info.width}px</span>
                            </div>
                            <div class="info-item">
                                <span class="info-label">高度:</span>
                                <span>${info.height}px</span>
                            </div>
                            <div class="info-item">
                                <span class="info-label">文件大小:</span>
                                <span>${(info.fileSize / 1024 / 1024).toFixed(2)}MB</span>
                            </div>
                        </div>
                        <div class="url-display">
                            ${window.location.origin}${API_BASE}/get?filename=${filename}
                            <button class="btn copy-btn" onclick="copyToClipboard('${window.location.origin}${API_BASE}/get?filename=${filename}')">
                                <i class="fas fa-copy"></i> 复制
                            </button>
                        </div>
                    `);
                    
                    // 显示图片预览
                    showImagePreview(filename);
                } else {
                    showResult(`<h3><i class="fas fa-exclamation-triangle"></i> 获取信息失败</h3><p>${result.message}</p>`, 'error');
                }
            } catch (error) {
                showResult(`<h3><i class="fas fa-exclamation-triangle"></i> 获取信息失败</h3><p>${error.message}</p>`, 'error');
            } finally {
                hideLoading();
            }
        }
        
        async function resizeImage() {
            const filename = document.getElementById('existingFilename').value.trim();
            const width = document.getElementById('targetWidth').value;
            const height = document.getElementById('targetHeight').value;
            const quality = document.getElementById('quality').value;
            
            if (!filename) {
                alert('请输入文件名');
                return;
            }
            
            showLoading();
            
            try {
                const response = await fetch(`${API_BASE}/resize`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                    },
                    body: `filename=${filename}&width=${width}&height=${height}&quality=${quality}`
                });
                
                const result = await response.json();
                
                if (result.code === 200) {
                    showResult(`
                        <h3><i class="fas fa-check-circle"></i> 图片处理成功</h3>
                        <p>新文件名: <strong>${result.data}</strong></p>
                        <p>目标尺寸: ${width}x${height}px</p>
                        <div class="url-display">
                            ${window.location.origin}${API_BASE}/get?filename=${result.data}
                            <button class="btn copy-btn" onclick="copyToClipboard('${window.location.origin}${API_BASE}/get?filename=${result.data}')">
                                <i class="fas fa-copy"></i> 复制
                            </button>
                        </div>
                    `);
                    
                    // 显示对比预览
                    showComparisonPreview(filename, result.data);
                } else {
                    showResult(`<h3><i class="fas fa-exclamation-triangle"></i> 处理失败</h3><p>${result.message}</p>`, 'error');
                }
            } catch (error) {
                showResult(`<h3><i class="fas fa-exclamation-triangle"></i> 处理失败</h3><p>${error.message}</p>`, 'error');
            } finally {
                hideLoading();
            }
        }
        
        async function enhanceImage() {
            const filename = document.getElementById('existingFilename').value.trim();
            
            if (!filename) {
                alert('请输入文件名');
                return;
            }
            
            showLoading();
            
            try {
                const response = await fetch(`${API_BASE}/enhance`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                    },
                    body: `filename=${filename}`
                });
                
                const result = await response.json();
                
                if (result.code === 200) {
                    showResult(`
                        <h3><i class="fas fa-magic"></i> 图片增强完成</h3>
                        <p>增强后文件名: <strong>${result.data}</strong></p>
                        <p>已优化分辨率和质量</p>
                        <div class="url-display">
                            ${window.location.origin}${API_BASE}/get?filename=${result.data}
                            <button class="btn copy-btn" onclick="copyToClipboard('${window.location.origin}${API_BASE}/get?filename=${result.data}')">
                                <i class="fas fa-copy"></i> 复制
                            </button>
                        </div>
                    `);
                    
                    // 显示对比预览
                    showComparisonPreview(filename, result.data);
                } else {
                    showResult(`<h3><i class="fas fa-exclamation-triangle"></i> 增强失败</h3><p>${result.message}</p>`, 'error');
                }
            } catch (error) {
                showResult(`<h3><i class="fas fa-exclamation-triangle"></i> 增强失败</h3><p>${error.message}</p>`, 'error');
            } finally {
                hideLoading();
            }
        }
        
        function showImagePreview(filename) {
            const previewSection = document.getElementById('previewSection');
            const originalPreview = document.getElementById('originalPreview');
            
            originalPreview.innerHTML = `
                <img src="${API_BASE}/get?filename=${filename}" alt="原图预览">
                <p>文件名: ${filename}</p>
            `;
            
            previewSection.style.display = 'block';
        }
        
        function showComparisonPreview(originalFilename, processedFilename) {
            const previewSection = document.getElementById('previewSection');
            const originalPreview = document.getElementById('originalPreview');
            const processedPreview = document.getElementById('processedPreview');
            
            originalPreview.innerHTML = `
                <img src="${API_BASE}/get?filename=${originalFilename}" alt="原图预览">
                <p>原图: ${originalFilename}</p>
            `;
            
            processedPreview.innerHTML = `
                <img src="${API_BASE}/get?filename=${processedFilename}" alt="处理后预览">
                <p>处理后: ${processedFilename}</p>
            `;
            
            previewSection.style.display = 'block';
        }
        
        function copyToClipboard(text) {
            navigator.clipboard.writeText(text).then(() => {
                alert('链接已复制到剪贴板');
            }).catch(err => {
                console.error('复制失败:', err);
            });
        }
        
        // 页面加载时预填入提供的文件名
        document.addEventListener('DOMContentLoaded', function() {
            // 如果URL中有文件名参数，自动填入
            const urlParams = new URLSearchParams(window.location.search);
            const filename = urlParams.get('filename');
            if (filename) {
                document.getElementById('existingFilename').value = filename;
            }
        });
    </script>
</body>
</html>